<template>
  <div class="app-container">
    <div class="ele-table-tool-default flex">
      <el-select v-model="params.status" placeholder="巡检状态" clearable filterable>
        <el-option v-for="(item,index) in statuses" :key="index" :value="item.value" :label="item.text" />
      </el-select>
      <el-button type="primary" class="ml12" @click="getAll">查询</el-button>
      <el-button type="primary" icon="el-icon-plus" class="mlauto" style="margin-left:auto;" @click="showForm">新增记录</el-button>
    </div>
    <el-table
      :data="tableData"
      border
      stripe
      style="width: 100%"
    >
      <el-table-column
        type="index"
        prop="date"
        label="序号"
        width="60"
        align="center"
      />
      <el-table-column
        prop="number"
        label="巡检单号"
        align="center"
        show-overflow-tooltip
      />
      <!-- <el-table-column
        prop="to_val"
        label="设备类型"
        align="center"
      /> -->
      <el-table-column
        prop="serialno"
        label="设备编号"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column
        prop="deviceName"
        label="设备名称"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column
        prop="status"
        label="巡检状态"
        align="center"
      >
        <template #default="scope">
          <el-tag v-if="+scope.row.status === 1" type="success">正常</el-tag>
          <el-tag v-else type="danger">故障</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="time"
        label="巡检时间"
        align="center"
        width="200"
      />
      <el-table-column
        prop="name"
        label="巡检人员"
        align="center"
      />
      <el-table-column
        prop="price"
        label="操作"
        align="center"
        width="160"
      >
        <template #default="scope">
          <el-link type="primary" icon="el-icon-edit" class="mr12" @click="edit(scope.row)">修改</el-link>
          <el-link type="danger" icon="el-icon-delete" @click="del(scope.row)">删除</el-link>
        </template>
      </el-table-column>
    </el-table>
    <main-form ref="mainForm" @submitForm="submitForm" />
  </div>
</template>

<script>
import MainForm from './components/main-form'
import { getAll, addOne, updateOne, deleteOne } from '@/api/inspection'
import { removeEmptyField } from '@/utils'
export default {
  components: {
    MainForm
  },
  data() {
    return {
      tableData: [],
      row: null,
      params: { status: '' },
      statuses: [{ value: 1, text: '正常' }, { value: 0, text: '异常' }]
    }
  },
  created() {
    this.getAll()
  },
  methods: {
    showForm() {
      this.$refs.mainForm.show()
    },
    async getAll() {
      const { data } = await getAll(removeEmptyField(this.params))
      this.tableData = data.data
    },
    submitForm(formData) {
      if (formData.id) {
        this.updateOne(formData)
      } else {
        this.addOne(formData)
      }
      this.getAll()
    },
    async addOne(formData) {
      const { msg } = await addOne(formData)
      this.$message.success(msg)
    },
    async updateOne(formData) {
      const { msg } = await updateOne(formData.id, formData)
      this.$message.success(msg)
    },
    async deleteOne(id) {
      const { msg } = await deleteOne(id)
      this.$message.success(msg)
    },
    edit(row) {
      this.$refs.mainForm.show(row)
    },
    del(row) {
      this.deleteOne(row.id)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
